using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for bzNewsOne
/// </summary>
public class bzService
{
	 private dbConnection _DB = null;
     private string _TableName = "bzService";
    private string _IDType = "_INT";
    private string _IDCol = "NewsOneID";
    private string _IDColCate = "CateID";
    private string _TableChild = "";
    private string _Field = "";
    public struct Enable
    {
        private static int _Enabled = 1;
        public static int Enabled { get { return _Enabled; } }

        private static int _Disabled = 0;
        public static int Disabled { get { return _Disabled; } }

        private static string _Cate = "1";
        public static string Cate { get { return _Cate; } }

        private static string _NotCate = "0";
        public static string NotCate { get { return _NotCate; } }
    }
    public struct NewsPosition
    {
        private static string _Home = "1";
        public static string Home { get { return _Home; } }

        private static string _In = "0";
        public static string In { get { return _In; } }
    }
    public bzService()
    {
        _DB = new dbConnection();
    }
    public DataTable getTable(string Key, string Enabled, string Page, string Limit, string HotNews,string CateID)
    {
        SqlParameter[] p = new SqlParameter[6];
        p[0] = new SqlParameter("@Key", "%" + Key + "%");
        p[1] = new SqlParameter("@Enabled", Enabled);
        p[2] = new SqlParameter("@Page", Page);
        p[3] = new SqlParameter("@Limit", Limit);
        p[4] = new SqlParameter("@HotNews", HotNews);
        p[5] = new SqlParameter("@CateID", CateID);
        return _DB.getDataTable(_TableName + "_search", p);

    }   
    public int changeHome(string ID)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@IDColActive", "HotNews");
        return _DB.updateData("bz_updateRecordStatus", p);
    }
    public long getTotal(string Key, string Enabled, string HotNews,string CateID)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@Key", "%" + Key + "%");
        p[1] = new SqlParameter("@Enabled", Enabled);
        p[2] = new SqlParameter("@HotNews", HotNews);
        p[3] = new SqlParameter("@CateID", CateID);
        return _DB.getTotalTable(_TableName + "_getTotal", p);
    }
    public DataTable getRecord(string ID, string Enabled)
    {
        SqlParameter[] p = new SqlParameter[4];
        p[0] = new SqlParameter("@TableName", _TableName);
        p[1] = new SqlParameter("@IDCol", _IDCol);
        p[2] = new SqlParameter("@IDValue", ID);
        p[3] = new SqlParameter("@Enabled", Enabled);
        return _DB.getDataTable("bz_getRecord" + _IDType, p);
    }
    public int deleteRecord(string[] ID)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@TableChild", _TableChild);
            p[2] = new SqlParameter("@IDCol", _IDCol);
            p[3] = new SqlParameter("@IDValue", str);
            count += _DB.updateData("bz_deleteRecord" + _IDType, p);
        }
        return count;
    }
    public int activeRecord(string[] ID, string Enabled)
    {
        int count = 0;
        foreach (string str in ID)
        {
            SqlParameter[] p = new SqlParameter[4];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", str);
            p[3] = new SqlParameter("@Enabled", Enabled);
            count += _DB.updateData("bz_activeRecord" + _IDType, p);
        }
        return count;
    }
    public int insertRecord(string TitleVN, string TitleEN, string DesVN, string DesEN, string ContentVN, string ContentEN, string PicThumb, string PicFull, string NoteVN, string NoteEN, string Enabled, string Relation,string CateID)
    {
        SqlParameter[] p = new SqlParameter[13];
        p[0] = new SqlParameter("@TitleVN", TitleVN);
        p[1] = new SqlParameter("@TitleEN", TitleEN);
        p[2] = new SqlParameter("@DesVN", DesVN);
        p[3] = new SqlParameter("@DesEN", DesEN);
        p[4] = new SqlParameter("@ContentVN", ContentVN);
        p[5] = new SqlParameter("@ContentEN", ContentEN);
        p[6] = new SqlParameter("@PicThumb", PicThumb);
        p[7] = new SqlParameter("@PicFull", PicFull);
        p[8] = new SqlParameter("@NoteVN", NoteVN);
        p[9] = new SqlParameter("@NoteEN", NoteEN);
        p[10] = new SqlParameter("@Enabled",Enabled);
        p[11] = new SqlParameter("@Relation", Relation);
        p[12] = new SqlParameter("@CateID", CateID);
        return _DB.updateData(_TableName + "_insert", p);
    }
    public int updateRecord(string ID, string TitleVN, string TitleEN, string DesVN, string DesEN, string ContentVN, string ContentEN, string PicThumb, string PicFull, string NoteVN, string NoteEN, string Enabled, string Relation,string CateID)
    {
        SqlParameter[] p = new SqlParameter[14];
        p[0] = new SqlParameter("@ID", ID);
        p[1] = new SqlParameter("@TitleVN", TitleVN);
        p[2] = new SqlParameter("@TitleEN", TitleEN);
        p[3] = new SqlParameter("@DesVN", DesVN);
        p[4] = new SqlParameter("@DesEN", DesEN);
        p[5] = new SqlParameter("@ContentVN", ContentVN);
        p[6] = new SqlParameter("@ContentEN", ContentEN);
        p[7] = new SqlParameter("@PicThumb", PicThumb);
        p[8] = new SqlParameter("@PicFull", PicFull);
        p[9] = new SqlParameter("@NoteVN", NoteVN);
        p[10] = new SqlParameter("@NoteEN", NoteEN);
        p[11] = new SqlParameter("@Enabled", Enabled);
        p[12] = new SqlParameter("@Relation", Relation);
        p[13] = new SqlParameter("@CateID", CateID);
        return _DB.updateData(_TableName + "_update", p);
    }
    public DataTable getTableOther(string ID, string Page, string Limit, string Enabled, string HotNews,string CateID)
    {
        SqlParameter[] p = new SqlParameter[6];
        p[0] = new SqlParameter("@ID", ID);
        p[1] = new SqlParameter("@Page", Page);
        p[2] = new SqlParameter("@Limit", Limit);
        p[3] = new SqlParameter("@Enabled", Enabled);
        p[4] = new SqlParameter("@HotNews", HotNews);
        p[5] = new SqlParameter("@CateID", CateID);
        return _DB.getDataTable(_TableName + "_other", p);
    }    
    public bool insertImage(string sFilePath, ref string sNameThumb, ref string sNameFull)
    {
        if (bzFile.Exist(sFilePath))
        {
            string arrRule = ".jpg.jpeg.bmp.png.gif";

            string[] strExt = sFilePath.Split('.');
            string ext = strExt[strExt.Length - 1].Trim().ToLower();
            if (arrRule.Contains("." + ext))
            {
                string sRan = DateTime.Now.ToString("ddmmyyyy_hhmmss");
                string sSuffix = "";
                string sSavePathThumb = HttpContext.Current.Server.MapPath("~") + "/Images/Service/";
                string sSavePathFull = HttpContext.Current.Server.MapPath("~") + "/Images/Service/";

                bzSiteOption op = new bzSiteOption(bzSiteOption.Group.Image, bzSiteOption.Note.About);
                int iThumbWidth = 0,
                    iThumbHeight = 0,
                    iFullWidth = 0,
                    iFullHeight = 0;
                try
                {
                    iThumbWidth = Convert.ToInt32(op.SiteOption["ImageSize_About_WidthThumb"]);
                    iThumbHeight = Convert.ToInt32(op.SiteOption["ImageSize_About_HeightThumb"]);
                    iFullWidth = Convert.ToInt32(op.SiteOption["ImageSize_About_WidthFull"]);
                    iFullHeight = Convert.ToInt32(op.SiteOption["ImageSize_About_HeightFull"]);
                }
                catch
                {
                    iThumbWidth = 0;
                    iThumbHeight = 0;
                    iFullWidth = 0;
                    iFullHeight = 0;
                }

                sNameThumb = bzFile.Prefix + "_" + sSuffix + "thumb_" + sRan + "." + ext;
                sNameFull = bzFile.Prefix + "_" + sSuffix + "full_" + sRan + "." + ext;
                while (bzFile.Exist(sSavePathThumb + sNameThumb) || bzFile.Exist(sSavePathFull + sNameFull))
                {
                    int i = new Random().Next(1, 10);
                    sNameFull = sNameFull.Replace(".", i + ".");
                    sNameThumb = sNameThumb.Replace(".", i + ".");
                }
                bool flg = bzImage.resizeImage(sFilePath, sSavePathThumb + sNameThumb, sSavePathFull + sNameFull, iThumbWidth, iThumbHeight, iFullWidth, iFullHeight, ext.Equals("gif"));
                bzFile.delete(sFilePath);
                return flg;
            }

        }
        return false;
    }
    public int moveCate(string[] arrID, string CateID)
    {
        int count = 0;
        foreach (string str in arrID)
        {
            SqlParameter[] p = new SqlParameter[5];
            p[0] = new SqlParameter("@TableName", _TableName);
            p[1] = new SqlParameter("@IDCol", _IDCol);
            p[2] = new SqlParameter("@IDValue", str);
            p[3] = new SqlParameter("@IDColParent", _IDColCate);
            p[4] = new SqlParameter("@IDValueParent", CateID);
            count += _DB.updateData("bz_updateCate" + _IDType, p);
        }
        return count;
    }  
}
